package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Menu;
import com.example.entity.Role;
import com.example.service.MenuService;
import com.example.service.RoleService;
import com.example.utils.TreeNode;
import com.example.vo.DataView;
import com.example.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/loadAllRole")
    public DataView loadAllRole(RoleVo roleVo){
        IPage<Role> page=new Page<>(roleVo.getPage(), roleVo.getLimit());
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getName()),"name",roleVo.getName());
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getRemark()),"remark",roleVo.getRemark());
        roleService.page(page,queryWrapper);
        return new DataView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("/addRole")
    public DataView addRole(Role role){
        DataView dataView=new DataView();
        roleService.save(role);
        dataView.setMsg("添加角色成功");
        dataView.setCode(200);
        return dataView;
    }

    @RequestMapping("/deleteRole")
    public DataView deleteRole(Role role){
        DataView dataView=new DataView();
        roleService.removeById(role.getId());
        dataView.setMsg("删除角色成功");
        dataView.setCode(200);
        return dataView;
    }

    @RequestMapping("/updateRole")
    public DataView updateRole(Role role){
        DataView dataView=new DataView();
        roleService.updateById(role);
        dataView.setMsg("修改角色成功");
        dataView.setCode(200);
        return dataView;
    }


    /**
     * 初始化下拉列表具有的权限
     */
    @RequestMapping("/initPermissionByRoleId")
    public DataView initPermissionByRoleId(Integer roleId){
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        List<Menu> allPermissions = menuService.list();
        List<Integer> currentRolePermissions = roleService.queryAllPermissionByRid(roleId);
        List<Menu> menus=null;
        if (currentRolePermissions.size()>0){
            queryWrapper.in("id",currentRolePermissions);
            menus = menuService.list(queryWrapper);
        }else{
            menus=new ArrayList<>();
        }
        //返回前台的格式，带有层级关系
        List<TreeNode> nodes = new ArrayList<>();
        for(Menu allPermission : allPermissions){
            String checkArr = "0";
            for (Menu currentPermisson : menus){
                if (allPermission.getId().equals(currentPermisson.getId())){
                    checkArr = "1";
                    break;
                }
            }
            Boolean spread = (allPermission.getOpen()==null || allPermission.getOpen()==1)?true:false;
            nodes.add(new TreeNode(allPermission.getId(), allPermission.getPid(), allPermission.getTitle(),spread,checkArr));
        }
        return new DataView(nodes);
    }

    /**
     * 保存权限插入数据库
     */
    @RequestMapping("/saveRolePermission")
    public DataView saveRolePermission(Integer rid,Integer[] mids){
        //根据rid删除之前的所有权限
        roleService.deleteRoleByRid(rid);
        if (mids != null && mids.length>0){
            for (Integer mid:mids){
                roleService.saveRoleMenu(rid,mid);
            }
        }
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("菜单权限分配成功");
        return dataView;
    }
}
